/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 16, 2012  3:25:18 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.promis.in.common
 * File Name : SupplierMasterAction.java
 * TODO
 */
package apps.promis.in.common;

import java.util.List;
import jpa.entitys.promis.common.SupplierGroupMaster;
import jpa.entitys.promis.common.SupplierMaster;
import jpa.entitys.synchro.staging.MmVendorMasInb;
import jpa.service.promis.common.SupplierMasterServices;
import jpa.service.synchro.staging.MmVendorMasInbServices;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import promis.utility.Utilitys;
import apps.sessionFactory;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:37:20 PM 
 * CreateBy=Chatree Siangklom
 * 
 * SupplierMasterAction
 * TODO
 */
public class SupplierMasterAction {
	
	
	private static final Logger log = Logger.getLogger(SupplierMasterAction.class);

	/**
	 * 
	 * Date =Nov 13, 2013 : 2:37:17 PM 
	 * CreateBy=Chatree Siangklom
	 * SupplierMasterAction
	 * TODO
	 */
	public SupplierMasterAction() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 * Date =Nov 13, 2013 : 2:37:31 PM 
	 * CreateBy=Chatree Siangklom
	 * SupplierMaster
	 * TODO
	 * @param supplier_code
	 * @return
	 * @throws Exception
	 */
	public SupplierMaster FindByUmCode(String supplier_code) throws Exception 
	{
		SupplierMasterServices cc = new SupplierMasterServices(sessionFactory.CreateEmPromis());
		
		return cc.findById(CommonService.IsNullRString(supplier_code));
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:37:33 PM 
	 * CreateBy=Chatree Siangklom
	 * boolean
	 * TODO
	 * @param supplier_code
	 * @return
	 * @throws Exception
	 */
	public boolean ExistingChecked(String supplier_code) throws Exception
	{
		return FindByUmCode(supplier_code) != null ? true : false;
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:37:38 PM 
	 * CreateBy=Chatree Siangklom
	 * SupplierMaster
	 * TODO
	 * @param mmVendorMasInb
	 * @return
	 * @throws Exception
	 */
	public SupplierMaster InsertOrUpdated(MmVendorMasInb mmVendorMasInb) throws Exception {
		SupplierMaster SupplierMasterObj = null;
		try{
		
			SupplierMasterServices supplierMasterServ = new SupplierMasterServices(sessionFactory.CreateEmPromis());
			SupplierMaster model = supplierMasterServ.findById(mmVendorMasInb.getSupplierCode());		
			SupplierGroupMaster supplierGroupMaster = new SupplierGroupMasterAction().InsertOrUpdateMmVendorGroup(mmVendorMasInb);
					
			if(model == null){
				
				model = new SupplierMaster();
				model.setSupplierCode(mmVendorMasInb.getSupplierCode());				
				model.setSupplierGroup(mmVendorMasInb.getSupplierGroup());
				model.setSupplierNameEnglish(mmVendorMasInb.getSupplierNameEnglish());
				model.setSupplierNameThai(mmVendorMasInb.getSupplierNameThai());
				
				model.setSupplierGroup(supplierGroupMaster.getSupplierGroup());
				
				model.setLocalImport(mmVendorMasInb.getLocalImport());
				model.setTypeOfSupplier(mmVendorMasInb.getTypeOfSupplier());
				model.setPrimaryLanguage("P");
				model.setStatus(mmVendorMasInb.getStatus());
				
				String temp = "";
				if(!CommonService.IsNull(mmVendorMasInb.getAddressText()))
					temp += mmVendorMasInb.getAddressText();
				
				if(!CommonService.IsNull(mmVendorMasInb.getCity()))
					temp += " " + mmVendorMasInb.getCity();
				
				if(!CommonService.IsNull(mmVendorMasInb.getTumbon()))
					temp += " " + mmVendorMasInb.getTumbon();
				
				if(!CommonService.IsNull(mmVendorMasInb.getProvince()))
					temp += " " + mmVendorMasInb.getProvince();
				
				if(!CommonService.IsNull(mmVendorMasInb.getZip()))
					temp += " " + mmVendorMasInb.getZip();
				
				model.setSupplierAddress(CommonService.IsNullRNull(temp));
				model.setTelephone(mmVendorMasInb.getPhone());
				model.setFax(mmVendorMasInb.getFax());
				
				model.setSyncExternalRefKey(mmVendorMasInb.getExtRefKey());
				model.setSyncExternalRefNo(mmVendorMasInb.getExtRefNo());
				
				model.setSyncIssync("N");
				model.setCreatedBy(mmVendorMasInb.getCreatedBy());
				model.setCreatedOn(Utilitys.ConvertStringToDateTime(mmVendorMasInb.getCreatedOn()));
				
				SupplierMasterObj = supplierMasterServ.Inserted(model);
				log.info("Insert Supplier Master = " + SupplierMasterObj.getSupplierCode());
				
			}else
			{			
				model.setSupplierCode(mmVendorMasInb.getSupplierCode());				
				model.setSupplierGroup(mmVendorMasInb.getSupplierGroup());
				model.setSupplierNameEnglish(mmVendorMasInb.getSupplierNameEnglish());
				model.setSupplierNameThai(mmVendorMasInb.getSupplierNameThai());
	
				model.setSupplierGroup(supplierGroupMaster.getSupplierGroup());
				
				model.setLocalImport(mmVendorMasInb.getLocalImport());
				model.setTypeOfSupplier(mmVendorMasInb.getTypeOfSupplier());
				model.setPrimaryLanguage("P");
				model.setStatus(mmVendorMasInb.getStatus());
				
				String temp = "";
				if(!CommonService.IsNull(mmVendorMasInb.getAddressText()))
					temp += CommonService.IsNullRString(mmVendorMasInb.getAddressText());
				
				if(!CommonService.IsNull(mmVendorMasInb.getCity()))
					temp += " " + CommonService.IsNullRString(mmVendorMasInb.getCity());
				
				if(!CommonService.IsNull(mmVendorMasInb.getTumbon()))
					temp += " " + CommonService.IsNullRString(mmVendorMasInb.getTumbon());
				
				if(!CommonService.IsNull(mmVendorMasInb.getProvince()))
					temp += " " + CommonService.IsNullRString(mmVendorMasInb.getProvince());
				
				if(!CommonService.IsNull(mmVendorMasInb.getZip()))
					temp += " " + CommonService.IsNullRString(mmVendorMasInb.getZip());
				
				model.setSupplierAddress(CommonService.IsNullRNull(temp));
				
				model.setTelephone(mmVendorMasInb.getPhone());
				model.setFax(mmVendorMasInb.getFax());
				
				model.setSyncExternalRefKey(mmVendorMasInb.getExtRefKey());
				model.setSyncExternalRefNo(mmVendorMasInb.getExtRefNo());
				
				model.setSyncIssync("N");
				if(CommonService.IsNull(mmVendorMasInb.getUpdatedOn()))
					model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmVendorMasInb.getCreatedOn()));				
				else
					model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmVendorMasInb.getUpdatedOn()));
				
				model.setLastUpdBy(CommonService.IsNullRNull(mmVendorMasInb.getUpdatedBy()));
				
				SupplierMasterObj = supplierMasterServ.Updated(model);
				log.info("Updated Supplier Master = " + SupplierMasterObj.getSupplierCode());
			}			
		
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
		return SupplierMasterObj;
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:37:44 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param lst
	 * @throws Exception
	 */
	public void CreateToPromis(List<MmVendorMasInb> lst) throws Exception 
	{
		try{
			if(!CommonService.IsNull(lst)){
				
				MmVendorMasInbServices mmVendorMasInbServ = new MmVendorMasInbServices(sessionFactory.CreateEmSynchro());
				
				for(MmVendorMasInb mmVendorMasInb : lst)
				{
					try{
						
						InsertOrUpdated(mmVendorMasInb);
						
						mmVendorMasInb.setIssync("Y");
						mmVendorMasInb.setMsgCode("S");
						mmVendorMasInb.setMsgDesc("Success");
						mmVendorMasInb.setMsgActResult(null);
						mmVendorMasInbServ.Updated(mmVendorMasInb);
						
					}catch(Exception ex)
					{
						mmVendorMasInb.setIssync("N");
						mmVendorMasInb.setMsgCode("E");
						mmVendorMasInb.setMsgDesc(CommonService.CutString(ex.getMessage(), 3500));
						mmVendorMasInb.setMsgActResult(CommonService.CutString(ex.getMessage(), 3500));
						mmVendorMasInbServ.Updated(mmVendorMasInb);
						
						log.error(ex);
					}			
				}
			}
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	@Deprecated
	/**
	 * 
	 * @param issync
	 * @throws Exception
	 * issync = N,Y,Null 
	 */
	public void ProcessingAll(String issync) throws Exception{
		
		MmVendorMasInbServices mmVendorMasInbServ = new MmVendorMasInbServices(sessionFactory.CreateEmSynchro());
		List<MmVendorMasInb> lst = null;
		
		if(!CommonService.IsNull(issync))
			lst = mmVendorMasInbServ.findAllIsSync("N");
		else
			lst = mmVendorMasInbServ.findByAll();
		
		if(!CommonService.IsNull(lst)){
			int i = 0;
			for(MmVendorMasInb mmVendorMasInb : lst)
			{
				i++;
				try{
					
					InsertOrUpdated(mmVendorMasInb);
					log.info(" Row Count = " + i);
					
					mmVendorMasInb.setIssync("Y");
					mmVendorMasInb.setMsgCode("S");
					mmVendorMasInb.setMsgDesc("Success");
					mmVendorMasInb.setMsgActResult(null);
					mmVendorMasInbServ.Updated(mmVendorMasInb);
					
				}catch(Exception ex)
				{
					mmVendorMasInb.setIssync("N");
					mmVendorMasInb.setMsgCode("E");
					mmVendorMasInb.setMsgDesc("Error");
					mmVendorMasInb.setMsgActResult(ex.getMessage());
					mmVendorMasInbServ.Updated(mmVendorMasInb);
					
					log.error(ex);
				}			
			}
		}
	}
	
	
//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//		
//		SupplierMasterAction xx = new SupplierMasterAction();
//		try {
//			
//			xx.Tested();
//			
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//	}

}
